package Com.Ring.TradingMobile;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import Com.Ring.TradingMobile.Lib.Common;
import Com.Ring.TradingMobile.SQLite.DBManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TradingMobileActivity extends Activity {
	/** Called when the activity is first created. */

	private static final String TAG = "TradingMobileTAG";


	private boolean IsRunning = true;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.d(TAG,"On Create");
		InitCreateDB();
		final Button btnDanhMuc = (Button) findViewById(R.id.btnDanhMuc);

		btnDanhMuc.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						DanhMucActivity.class);
				startActivity(intent);
				finish();
			}

		});
		final Button btnThiTruong = (Button) findViewById(R.id.btnThiTruong);

		btnThiTruong.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						ThiTruongActivity.class);
				Bundle senderBundle = new Bundle();
				
				senderBundle.putStringArray(Common.KeyIntentThiTruong,
						new String[]{"1", Common.DanhMucRoot,"0"});//1:	ma thi truong, danhmucroot: tendanhmuc, 0:	ActivityMain
				intent.putExtras(senderBundle);
				startActivity(intent);
				finish();
			}

		});
		final Button btnHistory = (Button) findViewById(R.id.btnHistory);

		btnHistory.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						HistoryActivity.class);
				startActivity(intent);
				finish();
			}

		});
		final Button btnSetting = (Button) findViewById(R.id.btnSetting);

		btnSetting.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						SettingActivity.class);
				startActivity(intent);
				finish();
			}

		});
		
		
		final Button btnExit = (Button) findViewById(R.id.btnBack);

		btnExit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}

		});
	}
	
//	public void doWork() {
//	    runOnUiThread(new Runnable() {
//	        public void run() {
//	            try{
//	                TextView txtCurrentTime= (TextView)findViewById(R.id.txtTimer);
//	                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
//	                Date date = new Date();
//	                txtCurrentTime.setText(dateFormat.format(date));
//	            }catch (Exception e) {}
//	        }
//	    });
//	}
//	class CountDownRunner implements Runnable{
//	    // @Override
//	    public void run() {
//	            while(IsRunning){
//	                try {
//	                doWork();
//	                    Thread.sleep(1000);
//	                    Log.d(TAG,"Retail");
//	                } catch (InterruptedException e) {
//	                        Thread.currentThread().interrupt();
//	                }catch(Exception e){
//	                }
//	                
//	            }
//	            Log.d(TAG,"Retail");
//	    }
//	}



	@Override
	protected void onStart() {
		Runnable runable = new Common.CountDownRunner();
		Common.StartTimer(runable,TradingMobileActivity.this);
		Log.d(TAG, "On Start");
		super.onStart();
	}

	@Override
	protected void onPause() {
		Common.StopTimer();
		Log.d(TAG, "On Pause");
		super.onPause();
	}
	

	@Override
	protected void onStop() {
		Log.d(TAG, "On Stop");
		super.onStop();
	}
	

	private void InitCreateDB() {
		DBManager db = new DBManager(this);
		try {
			db.InsertDanhMuc(Common.DanhMucRoot);
			db.InitDataConfig();
		} catch (Exception ex) {
			Log.d(TAG, ex.toString());
		} finally {
			db.close();
		}
	}

	@Override
	protected void onDestroy() {
		Log.d(TAG,"On Destroy");
		super.onDestroy();
	}

}